package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.Blog;
import com.situ.blog.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BlogDaoImpl implements IBlogDao {
    @Override
    public List<Blog> selectByPage(int offset, Integer limit) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,title,image,click_count,content,type_id,status,deleted,gmt_create,gmt_modified from t_blog limit ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, offset);
            statement.setInt(2, limit);
            System.out.println(statement);

            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String title = resultSet.getString("title");
                String image = resultSet.getString("image");
                int clickCount = resultSet.getInt("click_count");
                String content = resultSet.getString("content");
                int typeId = resultSet.getInt("type_id");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("deleted");
                // java.sql.Date
                Date gmtCreate = resultSet.getDate("gmt_create");
                Date gmtModified = resultSet.getDate("gmt_modified");

                Blog blog = new Blog(id, title, image, clickCount, content, typeId, status, deleted, gmtCreate, gmtModified);
                list.add(blog);
            }

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return list;
    }

    @Override
    public int selectTotalCount() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from t_blog ";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getInt(1);
            }

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }

        return count;
    }

    @Override
    public int add(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into t_blog(title,type_id,image,content) values(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setInt(2, blog.getTypeId());
            statement.setString(3, blog.getImage());
            statement.setString(4, blog.getContent());
            System.out.println(statement);
            count = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }

        return count;
    }
}
